package etc.server.csp;


import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.dmz.test.etc.domain.MsgHeader;
import com.dmz.test.etc.utils.etc.DateTools;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Data
@AllArgsConstructor
public class CspPack {

    private final static String encoding = "GBK";
    private String ip;
    private int port;

    public static String headSet(String reqbody, String reqtype, int bodyconlen, int id) {
        String verifyCode = SecureUtil.md5(reqbody);

        MsgHeader phr = new MsgHeader();
        phr.setMessageId(StrUtil.padPre(String.valueOf(id), 4, "0"));
        int totalLen = 54 + bodyconlen;
        phr.setMessageLength(StrUtil.padPre(String.valueOf(totalLen), 4, "0"));
        phr.setMessageType(reqtype);
        phr.setReceiverId("1400000");
        phr.setSenderId("8000001");
        phr.setVerifyCode(verifyCode.toUpperCase());
        return phr.toString();
    }

    public static String packSet(String reqbody, String reqType, int id)
            throws Exception {
        byte[] reqBodyBytes = reqbody.getBytes(encoding);
        int bodyconlen = Math.round((reqBodyBytes.length >> 3) + 1) << 3;
        if (bodyconlen > reqBodyBytes.length) {
            reqbody = StrUtil.padAfter(reqbody, bodyconlen, " ");
        }
        String reqHead = headSet(reqbody, reqType, bodyconlen, id);
        return reqHead + reqbody;
    }


    public String send1001(String msg) {
        StringBuilder reqBody = new StringBuilder(DateTools.getDateTimeString());
        log.info("client--1001--req--body:{}", reqBody.toString());
        try {
            String reqMsg = packSet(reqBody.toString(), "1001", 1);
            log.info("1001--req--data:{}", reqMsg);
            byte[] response = CspClient.socket(reqMsg, ip, port);

            if (response != null) {
                log.info("client--1002--res--body--:{}", new String(response, encoding));
            }
            return send1003(3, msg);

        } catch (Exception e) {
            log.error("报文1001发送异常！", e);
            throw new RuntimeException(e);
        } finally {
            CspClient.close();
        }
    }

    public String send1003(int id, String msg) {
        StringBuilder sb = new StringBuilder();
        String dts = DateTools.getDateTimeString();
        sb.append(dts);
        sb.append("8000123");
        sb.append("8000123001");
        log.info("client--1003--req--body:{}", sb.toString());
        try {
            String reqMsg = packSet(sb.toString(), "1003", id);
            byte[] response = CspClient.socket(reqMsg, ip, port);
            String resBody = new String(response, encoding);
            log.info("client--1003--res--body--dec:{}", resBody);
            return send(id + 2, msg);
        } catch (Exception e) {
            log.error("报文1003发送异常！", e);
            throw new RuntimeException(e);
        }
    }

    public String send(int n, String msg) throws Exception {
        String reqBody = msg.substring(18);
        log.info("client--receive--body:{}", reqBody);
        log.info("client--receive--body--length:{}", reqBody.length());
        String reqHead = msg.substring(0, 18);
        String msgType = reqHead.substring(14, 18);
        try {
            String reqMsg = packSet(reqBody, msgType, n);
            byte[] response = CspClient.socket(reqMsg, ip, port);
            String resBody = new String(response, encoding);
            log.info("client--{}--res--body:{}", msgType, resBody);
            return resBody;
        } catch (Exception e) {
            log.error("报文{}发送异常！", msgType, e);
            throw new RuntimeException(e);
        }
    }

}
